package com.csy.stu.dao.impl;

import com.csy.stu.dao.StudentDao;
import com.csy.stu.entity.Student;
import com.csy.stu.util.DbUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author CSY
 * @date 2025 - 01 - 21
 */
public class StudentDaoImpl implements StudentDao {

    @Override
    public List<Student> findAll() {
        List<Student> list = new ArrayList<>();
        String sql = "select * from student";
        Connection conn = DbUtil.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                Student s = new Student();
                s.setAge(rs.getInt("age"));
                s.setName(rs.getString("name"));
                s.setPhone(rs.getString("phone"));
                s.setSex(rs.getString("sex"));
                s.setStuId(rs.getInt("stuId"));
                list.add(s);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DbUtil.close(conn, ps, rs);
        }
        return list;
    }

    @Override
    public int deleteById(int id) {
        int n = 0;
        String sql = "delete from student where stuId = ?";
        Connection conn = DbUtil.getConnection();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1, id);
            n = ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DbUtil.close(conn, ps, null);
        }
        return n;
    }

    @Override
    public int insert(Student student) {
        int n = 0;
        String sql = "insert student(name,age,sex,phone) values(?,?,?,?)";
        Connection conn = DbUtil.getConnection();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1,student.getName());
            ps.setInt(2,student.getAge());
            ps.setString(3,student.getSex());
            ps.setString(4,student.getPhone());
            n = ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DbUtil.close(conn, ps, null);
        }
        return n;
    }

    @Override
    public int update(Student student) {
        int n = 0;
        String sql = "update student set name=?,age=?,sex=?,phone=? where stuId = ?";
        Connection conn = DbUtil.getConnection();
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1,student.getName());
            ps.setInt(2,student.getAge());
            ps.setString(3,student.getSex());
            ps.setString(4,student.getPhone());
            ps.setInt(5,student.getStuId());
            n = ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DbUtil.close(conn, ps, null);
        }
        return n;
    }
}
